package com.zst.http.forward.utils;

import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URL;

public class InetSocketAddressResolver {
    public static InetSocketAddress resolveFromURL(String url) throws MalformedURLException {
        if (url == null || url.isEmpty()) {
            return null;
        }

        URL urlObj = new URL(url);

        String host = urlObj.getHost();
        int port = urlObj.getPort();

        if (port == -1) {
            switch (urlObj.getProtocol()) {
                case "http":
                    port = 80;
                    break;
                case "https":
                    port = 443;
                    break;
                default:
                    throw new RuntimeException("url协议无效");
            }
        }

        return new InetSocketAddress(host, port);
    }
}
